//
//  ZPBaseViewController.m
//  ZPIOSEducation
//
//  Created by mini1 on 2016/11/29.
//  Copyright © 2016年 mini1. All rights reserved.
//



#import "ZPBaseViewController.h"
#import "ZPNoDataView.h"

#define NoNetWorkViewImvTag       1000
#define NoNetWorkViewTitleTag     2000
#define NoNetWorkViewSubTitleTag  2001

@interface ZPBaseViewController ()

@end

@implementation ZPBaseViewController


- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = zFreeColor(238, 238, 238, 1);
    
    hud = [[MBProgressHUD alloc] initWithView:self.view];
    [hud showAnimated:YES];
    hud.hidden = YES;
    hud.label.text = @"加载中...";
    [self.view addSubview:hud];
}

- (BOOL)isHudShow
{
    return isHudShow;
}

#pragma mark - 网络请求Activity加载效果
- (void)showHudithString:(NSString *)title
{
    hud.label.text = [NSString  stringWithFormat:@"%@",title];
    hud.hidden = NO;
    isHudShow = YES;
}

- (void)hiddenHud
{
    hud.hidden = YES;
    isHudShow = NO;
}

#pragma mark - 数据个数为0视图
- (void)showNoDataViewWithTitle:(NSString *)title
{
    if (noDataView) {
        
    }else {
        noDataView = [[[NSBundle mainBundle] loadNibNamed:@"NoDataView" owner:self options:nil] lastObject];
        noDataView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64);
        [self.view  addSubview:noDataView];
    }
    UILabel  *titleLab = (UILabel  *)[noDataView  viewWithTag:1000];
    titleLab.text = [NSString  stringWithFormat:@"%@",title];
    
    noDataView.hidden = NO;
    [self.view  bringSubviewToFront:noDataView];
}

- (void)hiddenNoDataView
{
    if (noDataView) {
        noDataView.hidden = YES;
    }
}

#pragma mark - 没有网络或服务器出错
- (void)showNoNetWorkView
{
    if (noNetWorkView) {
        
    }else {
        noNetWorkView = [[UIView  alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64)];
        noNetWorkView.backgroundColor = [UIColor  whiteColor];
        
        UIImageView  *imageView = [[UIImageView  alloc] initWithFrame:CGRectMake((noNetWorkView.frame.size.width - 118) / 2., 70, 118, 105)];
        imageView.userInteractionEnabled = YES;
        imageView.tag = NoNetWorkViewImvTag;
        [noNetWorkView  addSubview:imageView];
        
        UILabel  *titleLab = [[UILabel  alloc] initWithFrame:CGRectMake((noNetWorkView.frame.size.width - 200) / 2., imageView.frame.origin.y + imageView.frame.size.height + 30, 200, 25)];
        titleLab.font = [UIFont  systemFontOfSize:20.];
        titleLab.textAlignment = NSTextAlignmentCenter;
        titleLab.textColor = UIColorFromRGB(0x333333);
        titleLab.tag = NoNetWorkViewTitleTag;
        [noNetWorkView  addSubview:titleLab];
        
        UILabel  *subTitleLab = [[UILabel  alloc] initWithFrame:CGRectMake((noNetWorkView.frame.size.width - 200) / 2., titleLab.frame.origin.y + titleLab.frame.size.height + 10, 200, 23)];
        subTitleLab.font = [UIFont  systemFontOfSize:18.];
        subTitleLab.textAlignment = NSTextAlignmentCenter;
        subTitleLab.textColor = UIColorFromRGB(0x999999);
        subTitleLab.tag = NoNetWorkViewSubTitleTag;
        [noNetWorkView  addSubview:subTitleLab];
        
        UIButton  *reloadBtn = [UIButton  buttonWithType:UIButtonTypeCustom];
        reloadBtn.frame = CGRectMake((noNetWorkView.frame.size.width - 156) / 2., subTitleLab.frame.origin.y + subTitleLab.frame.size.height + 35, 156, 36);
        [reloadBtn  setBackgroundColor:UIColorFromRGB(0xeeeeee)];
        [reloadBtn  setTitle:@"重新加载" forState:UIControlStateNormal];
        [reloadBtn  setTitleColor:UIColorFromRGB(0x21a4eb) forState:UIControlStateNormal];
        [reloadBtn.titleLabel  setFont:[UIFont  systemFontOfSize:14.]];
        reloadBtn.layer.cornerRadius = 4.;
        reloadBtn.layer.borderColor = [UIColor  colorWithRed:238/255. green:238/255. blue:238/255. alpha:1.].CGColor;
        reloadBtn.layer.borderWidth = 1.;
        reloadBtn.layer.masksToBounds = YES;
        [reloadBtn  addTarget:self action:@selector(reloadButtonClick) forControlEvents:UIControlEventTouchUpInside];
        [noNetWorkView  addSubview:reloadBtn];
        
        [self.view  addSubview:noNetWorkView];
        
    }
    
    UIImageView  *tempImv = (UIImageView  *)[noNetWorkView  viewWithTag:NoNetWorkViewImvTag];
    UILabel  *tempLab = (UILabel  *)[noNetWorkView  viewWithTag:NoNetWorkViewTitleTag];
    UILabel  *tempSubLab = (UILabel  *)[noNetWorkView  viewWithTag:NoNetWorkViewSubTitleTag];
    
    BOOL  hasNetWork = ((ZPAppDelegate *)[[UIApplication sharedApplication] delegate]).isHasNetwork;
    if (hasNetWork) {
        tempImv.image = [UIImage  imageNamed:@"serviceE"];
        tempLab.text = @"哎呀，系统小抽了一下~";
        tempSubLab.text = @"请稍后重新加载";
    }else {
        tempImv.image = [UIImage  imageNamed:@"networkE"];
        tempLab.text = @"网络请求失败";
        tempSubLab.text = @"请检查您的网络设置";
    }
    
    noNetWorkView.hidden = NO;
    [self.view bringSubviewToFront:noNetWorkView];
}

- (void)reloadButtonClick
{
    noNetWorkView.hidden = YES;
    [self  reloadButtonAction];
}

- (void)reloadButtonAction
{
    
}

- (void)hiddenNoNetWorkView
{
    if (noNetWorkView) {
        noNetWorkView.hidden = YES;
    }
}

@end
